package com.boot.netty.demo2;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyClientDemo {
    /**
     * 1.创建线程组
     * 2.设置线程组启动助手 bootstrap
     * 3.设置客户端通道为NIO
     * 4.创建通道初始化对象
     * 5.向pipeline中添加自定义业务处理的handler
     * 6.启动客户端，等待链接服务端，同时将异步改为同步
     * 8.关闭通道和连接池
     */

    public static void main(String[] args) throws InterruptedException {

        //1.创建线程组
        EventLoopGroup group=new NioEventLoopGroup();
        //2.创建bootstrap
        Bootstrap bootstrap=new Bootstrap();
        bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new NettyClientHandlerDemo());
                    }
                });
        //启动客户端
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9999).sync();
        //关闭通道和连接池
        //监听通道关闭的状态事件
        channelFuture.channel().closeFuture().sync();
        group.shutdownGracefully();

    }




}


